#ifndef LILANMANGECLIENT_H
#define LILANMANGECLIENT_H

#include <QObject>
#include <QUdpSocket>
#include <QTimer>
#include <QHash>
#include <QString>
#include <QHostAddress>
#include <QTextCodec>
#include <QTime>
#include <QDebug>
#include "litcpsocketstring.h"
#include "litcpsocketfileserver.h"
#include "litcpsocketfileclient.h"

class LiLANMangeClient : public QObject
{
    Q_OBJECT
public:
    LiLANMangeClient(QObject *parent = 0);
    //广播信息
    Q_INVOKABLE void broadcastMessage(QString str);//广播信息
    Q_INVOKABLE void broadcastMessageAllAccurate(QString str);//广播信息到all指定地址和端口
    Q_INVOKABLE void broadcastMessageAllPort(QString str,QString address="255.255.255.255");//广播信息到all指定端口
    Q_INVOKABLE void broadcastMessageToOne(quint16 port,QString str);
    Q_INVOKABLE void broadcastMessageToOne(QString address,qint16 port,QString str);
    //添加广播对象,在广播信息时会向添加的对象发送
    Q_INVOKABLE bool addBroadcastGoal(QString senderAddress,quint16 senderPort);
    //添加广播端口号
    Q_INVOKABLE void addBroadcastPort(quint16 addPort);
    //第一步呼叫服务端
    Q_INVOKABLE void callServerTimerStart(int cycle=2000,int sustain=30000);
    Q_INVOKABLE void handleRootUdpNews(QString str,QHostAddress &sendAddress);
    Q_INVOKABLE void connectToServe(QString address,quint16 port);
    //Tcp信息发送
    Q_INVOKABLE void sendTcpNews(int socketDescript,QString str);
    Q_INVOKABLE void sendTcpNews(QString goalName,QString str);
    Q_INVOKABLE void sendTcpNews(LiTcpSocketString *tcpsocket,QString str);
    Q_INVOKABLE void registerTcpToGoal(LiTcpSocketString *tcpsocket);

    //    文件
//    Q_INVOKABLE bool fileTcpListen(LiTcpSocketFileServer *tcpSocketFileServer,QHostAddress address = QHostAddress::Any,quint16 port = 0);

    Q_INVOKABLE void sendTcpFile(QString fileName,LiTcpSocketString *tcpSocketStr);
    Q_INVOKABLE void sendTcpFile(QString fileName,int tcpDescriptor);
    Q_INVOKABLE void handleTcpNews(qintptr tcpSocketDescript,QString tcpNews);

    // 返回本机IP地址
    Q_INVOKABLE QString getLocalIPAddress();

    //属性基本修改
    Q_INVOKABLE int returnServerRegisteryCount();
    Q_INVOKABLE QString returnServerRegisteryKey(int index);
    Q_INVOKABLE QString returnServerRegisteryValue(int index);
    Q_INVOKABLE QString returnServerRegisteryName(int index);//在value中的值
    Q_INVOKABLE int returnServerRegisteryPort(int index);//在value中的值

    Q_INVOKABLE int returnTcpSocketRegisteryCount();
    Q_INVOKABLE int returnTcpSocketRegisteryKey(int index);
    //通过goalName查询其描述符,没有返回-1
    Q_INVOKABLE int returnDescriptorFromGoalName(QString goalName);




    //kinect zi liang
//    Q_INVOKABLE void autoConnectServer();

signals:

    void udpNews(QString str);
    void updateServerRegistery();
    void updateTcpSocketRegistery();
    void tcpNewsKinect(QString str);
    //    文件传输完成信号初级--项目需要（后期升级）
    void receiveFileOver(QString filePath);


    //测试
    void debug(QString str);
public slots:
    void callServer();//服务器在吗
    void callServerTimerStop();//停止呼叫
    void onRootUdpNews();//信息接受
    void onRegistTcpSocket(LiTcpSocketString *fileTcpSocket);//登记成功的连接
    void onTcpNews(qintptr tcpSocketDescript);
    void onRegistTcpSocketFile(LiTcpSocketFileClient *fileTcpSocket);

    void onDeleteTcpSocketFileClient(qintptr socketDescript);
    void onDeleteTcpSocketFileServer(quint32 id);
    //删除字符传输tcp套接字
    void onDeleteTcpSocketString(qintptr socketDescript);




    void autoConnectServer();
private:
    static quint16 allPort;
    static quint32 allFileServerID;
    void addAllPort();//端口号增加函数
    void addAllFileServerID();

    QUdpSocket *rootUdpSocket;
    quint16 rootUdpPort;
    QString multicastAddress;
    quint16 goalPort;

    QTextCodec *textCode;//发送字符串编码
    QString myName;
    QTimer *callServerTimer;
    QTimer *callServerRange;//呼叫持续时间
    QString tempRootStr;
    QHostAddress senderAddress;
     quint16 senderPort;

     QString localIPV4Address;//本地IPV4地址

     QHash<QString,QString> *serverRegistery;//服务器列表
     QHash<qintptr,QString> *tcpNewsList;//tcp信息列表
     QHash<qintptr,LiTcpSocketString*> *tcpSocketRegistery;//tcpsocket列表
     QHash<qintptr,LiTcpSocketFileClient*> *tcpSocketFileRegistery;//文件传输客户端注册表
     QHash<quint32,LiTcpSocketFileServer*> *tcpSocketFileServerRegistery;//文件传输服务器注册表
     QHash<QHostAddress*,quint16*> *udpaClientRegistery;//UDP广播目标注册表
     QList<quint16> *udpGoalPortList;//udp广播端口号注册表
};

#endif // LILANMANGECLIENT_H
